﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using PagedList;
using DLL;
using CommonHelpers;

namespace QuanLyBanLe
{
    public partial class FormProductInDetailHistory : Form
    {
        private InProductService m_InProductService;
        private OutProductService m_OutProductService;
        private ProductService m_ProductService;
        private List<pInProdHistory_Get_Result> m_InProductDetailsHistory;
        private List<pOutProdHistory_Get_Result> m_OutProductDetailsHistory;
        private List<pSellProductInDay_Result> m_SellProductHistory;

        private int m_PageNumber;
        private int m_PageSize;
        private int m_MaxPage;
        private bool m_EnabledEvent;
        private int m_TotalRecord;

        private int m_ProductId;

        private int m_PageNumberOut;
        private int m_PageSizeOut;
        private int m_MaxPageOut;
        private int m_TotalRecordOut;
        private bool m_EnabledOutProductEvent;

        private int m_SellPageNumber;
        private int m_SellPageSize;
        private int m_SellMaxPage;
        private int m_SellTotalRecord;
        private bool m_EnabledSellEvent;        

        public FormProductInDetailHistory(int productId)
        {
            InitializeComponent();
            m_ProductId = productId;
            m_InProductService = new InProductService();
            m_OutProductService = new OutProductService();
            m_PageNumber = 1;
            m_PageSize = 20;
            m_PageNumberOut = 1;
            m_PageSizeOut = 20;
            m_SellPageNumber = 1;
            m_SellPageSize = 20;
            
            m_EnabledEvent = false;
            m_EnabledOutProductEvent = false;
            m_EnabledSellEvent = false;
            m_ProductService = new ProductService();
            lblTotalInMoney.Text = "0 Đ";
            lblTotalOutAmount.Text = "0 Đ";
            lblTotalSellAmount.Text = "0";
            dtpInProduct.AutoGenerateColumns = false;
            dtgProductOut.AutoGenerateColumns = false;
        }

        private void FormProductInDetailHistory_Load(object sender, EventArgs e)
        {
            m_InProductDetailsHistory = m_InProductService.GetInProductHistory(m_ProductId, null, null);
            m_OutProductDetailsHistory = m_OutProductService.GetOutProductHistory(m_ProductId, null, null);
            m_SellProductHistory = m_ProductService.GetSellProductInDay(m_ProductId, null, null);
            CalculateTotalInProduct();
            BindDetailInfoToGrid();

            CalculateTotalOutAmount();
            BindOutDetailInfoToGrid();

            CalculateTotalSellAmount();
            BindSellHistoryToGrid();

            this.Text += FormMain.GetShopInfo();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #region In product
        private void btnFirst_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            ChangePageInProdDetail();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber > 1 ? m_PageNumber - 1 : m_PageNumber;
            ChangePageInProdDetail();
        }

        private void mnrPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledEvent)
            {
                m_PageNumber = Convert.ToInt32(mnrPageIndex.Value);
                ChangePageInProdDetail();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_PageNumber < m_MaxPage ? m_PageNumber + 1 : m_PageNumber;
            ChangePageInProdDetail();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            m_PageNumber = m_MaxPage;
            ChangePageInProdDetail();
        }

        private void SetEnabledForPagingNavigator()
        {
            if (m_PageNumber == m_MaxPage)
            {
                if (m_PageNumber == 1)
                {
                    btnFirst.Enabled = false;
                    btnPrev.Enabled = false;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
                else
                {
                    btnFirst.Enabled = true;
                    btnPrev.Enabled = true;
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (m_PageNumber == 1)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
            else
            {
                btnFirst.Enabled = true;
                btnPrev.Enabled = true;
                btnLast.Enabled = true;
                btnNext.Enabled = true;
            }
        }

        private void ChangePageInProdDetail()
        {
            BindDetailInfoToGrid();
        }

        private void BindDetailInfoToGrid()
        {
            var inProductsHistory = m_InProductDetailsHistory.ToPagedList(m_PageNumber, m_PageSize);
            dtpInProduct.DataSource = inProductsHistory.ToList();
            m_TotalRecord = inProductsHistory.TotalItemCount;
            m_MaxPage = inProductsHistory.PageCount;
            if (m_TotalRecord > 0)
            {
                lblTotalPage.Text = "/" + m_MaxPage.ToString();
                mnrPageIndex.Minimum = 1;
                mnrPageIndex.Enabled = true;
                mnrPageIndex.Maximum = m_MaxPage;
                mnrPageIndex.Value = m_PageNumber;
                SetEnabledForPagingNavigator();
            }
            else
            {
                m_PageNumber = 0;
                mnrPageIndex.Minimum = 0;
                mnrPageIndex.Value = 0;
                mnrPageIndex.Enabled = false;
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                btnLast.Enabled = false;
                btnNext.Enabled = false;
                lblTotalPage.Text = string.Empty;
            }

            m_EnabledEvent = true;
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            m_EnabledEvent = false;
            m_PageNumber = 1;
            m_InProductDetailsHistory = m_InProductService.GetInProductHistory(m_ProductId, dtpFromDate.Value.Date, dtpToDate.Value.Date);
            CalculateTotalInProduct();
            BindDetailInfoToGrid();
        }

        private void CalculateTotalInProduct()
        {
            long totalAmount = 0;
            long totalMoney = 0;
            foreach (var detail in m_InProductDetailsHistory)
            {
                totalAmount += detail.Amount;
                totalMoney += detail.TotalInputPrice.Value;
            }
            lblTotalInAmount.Text = totalAmount.ToString();
            lblTotalInMoney.Text = totalMoney.ToMoney();
        }

        private void btnAllInProduct_Click(object sender, EventArgs e)
        {
            m_PageNumber = 1;
            m_InProductDetailsHistory = m_InProductService.GetInProductHistory(m_ProductId, null, null);
            CalculateTotalInProduct();
            BindDetailInfoToGrid();
        }
        #endregion In product

        #region Out product
        private void btnFilterOut_Click(object sender, EventArgs e)
        {
            m_EnabledOutProductEvent = false;
            m_PageNumberOut = 1;
            m_OutProductDetailsHistory = m_OutProductService.GetOutProductHistory(m_ProductId, dtFromDateOut.Value.Date, dtToDateOut.Value.Date);
            CalculateTotalOutAmount();
            BindOutDetailInfoToGrid();
        }

        private void btnFirstOut_Click(object sender, EventArgs e)
        {
            m_PageNumberOut = 1;
            ChangePageOutProdDetail();
        }

        private void btnPrevOut_Click(object sender, EventArgs e)
        {
            m_PageNumberOut = m_PageNumberOut > 1 ? m_PageNumberOut - 1 : m_PageNumberOut;
            ChangePageOutProdDetail();
        }

        private void mnrPageIndexOut_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledOutProductEvent)
            {
                m_PageNumberOut = Convert.ToInt32(mnrPageIndexOut.Value);
                ChangePageOutProdDetail();
            }
        }

        private void btnNextOut_Click(object sender, EventArgs e)
        {
            m_PageNumberOut = m_PageNumberOut < m_MaxPageOut ? m_PageNumberOut + 1 : m_PageNumberOut;
            ChangePageOutProdDetail();
        }

        private void btnLastOut_Click(object sender, EventArgs e)
        {
            m_PageNumberOut = m_MaxPageOut;
            ChangePageOutProdDetail();
        }

        private void ChangePageOutProdDetail()
        {
            BindOutDetailInfoToGrid();
        }

        private void BindOutDetailInfoToGrid()
        {
            var outProductsHistory = m_OutProductDetailsHistory.ToPagedList(m_PageNumberOut, m_PageSizeOut);
            dtgProductOut.DataSource = outProductsHistory.ToList();
            m_TotalRecordOut = outProductsHistory.TotalItemCount;
            m_MaxPageOut = outProductsHistory.PageCount;
            if (m_TotalRecordOut > 0)
            {
                lblTotalPageOut.Text = "/" + m_MaxPageOut.ToString();
                mnrPageIndexOut.Minimum = 1;
                mnrPageIndexOut.Enabled = true;
                mnrPageIndexOut.Maximum = m_MaxPageOut;
                mnrPageIndexOut.Value = m_PageNumberOut;
                SetEnabledForPagingNavigatorOutProduct();
            }
            else
            {
                m_PageNumberOut = 0;
                mnrPageIndexOut.Minimum = 0;
                mnrPageIndexOut.Value = 0;
                mnrPageIndexOut.Enabled = false;
                btnFirstOut.Enabled = false;
                btnPrevOut.Enabled = false;
                btnLastOut.Enabled = false;
                btnNextOut.Enabled = false;
                lblTotalPageOut.Text = string.Empty;
            }

            m_EnabledOutProductEvent = true;
        }

        private void SetEnabledForPagingNavigatorOutProduct()
        {
            if (m_PageNumberOut == m_MaxPageOut)
            {
                if (m_PageNumberOut == 1)
                {
                    btnFirstOut.Enabled = false;
                    btnPrevOut.Enabled = false;
                    btnLastOut.Enabled = false;
                    btnNextOut.Enabled = false;
                }
                else
                {
                    btnFirstOut.Enabled = true;
                    btnPrevOut.Enabled = true;
                    btnLastOut.Enabled = false;
                    btnNextOut.Enabled = false;
                }
            }
            else if (m_PageNumberOut == 1)
            {
                btnFirstOut.Enabled = false;
                btnPrevOut.Enabled = false;
                btnLastOut.Enabled = true;
                btnNextOut.Enabled = true;
            }
            else
            {
                btnFirstOut.Enabled = true;
                btnPrevOut.Enabled = true;
                btnLastOut.Enabled = true;
                btnNextOut.Enabled = true;
            }
        }

        private void btnAllOutProduct_Click(object sender, EventArgs e)
        {
            m_PageNumberOut = 1;
            m_OutProductDetailsHistory = m_OutProductService.GetOutProductHistory(m_ProductId, null, null);
            CalculateTotalOutAmount();
            BindOutDetailInfoToGrid();
        }

        private void tcInOutProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            var control = (TabControl)sender;
            if (control.SelectedIndex == 1 && dtgProductOut.Rows.Count == 0)
            {
                dtgProductOut.RefreshEdit();
            }
        }

        private void CalculateTotalOutAmount()
        {
            long amount = 0;
            foreach (var detail in m_OutProductDetailsHistory)
            {
                amount += detail.Amount;
            }
            lblTotalOutAmount.Text = amount.ToString();
        }
        #endregion Out product

        #region Sell Product
        private void CalculateTotalSellAmount()
        {
            int totalSell = 0;
            foreach (pSellProductInDay_Result sell in m_SellProductHistory)
            {
                totalSell += sell.Amount;
            }
            lblTotalSellAmount.Text = totalSell.ToString();
        }

        private void BindSellHistoryToGrid()
        {
            var sellHistory = m_SellProductHistory.ToPagedList(m_SellPageNumber, m_SellPageSize);
            dtgSell.DataSource = sellHistory.ToList();
            m_SellTotalRecord = sellHistory.TotalItemCount;
            m_SellMaxPage = sellHistory.PageCount;
            if (m_SellTotalRecord > 0)
            {
                lblSellTotalPage.Text = "/" + m_SellMaxPage.ToString();
                mnrSellPageIndex.Minimum = 1;
                mnrSellPageIndex.Enabled = true;
                mnrSellPageIndex.Maximum = m_SellMaxPage;
                mnrSellPageIndex.Value = m_SellPageNumber;
                SetEnabledForSellPagingNavigator();
            }
            else
            {
                m_SellPageNumber = 0;
                mnrSellPageIndex.Minimum = 0;
                mnrSellPageIndex.Value = 0;
                mnrSellPageIndex.Enabled = false;
                btnSellFirst.Enabled = false;
                btnSellPrev.Enabled = false;
                btnSellLast.Enabled = false;
                btnSellNext.Enabled = false;
                lblSellTotalPage.Text = string.Empty;
            }

            m_EnabledSellEvent = true;
        }

        private void btnSellFilter_Click(object sender, EventArgs e)
        {
            m_EnabledSellEvent = false;
            m_SellPageNumber = 1;
            m_SellProductHistory = m_ProductService.GetSellProductInDay(m_ProductId, dtpSellFromDate.Value.Date, dtpSellToDate.Value.Date);
            CalculateTotalSellAmount();
            BindSellHistoryToGrid();
        }

        private void btnSellGetAll_Click(object sender, EventArgs e)
        {
            m_EnabledSellEvent = false;
            m_SellPageNumber = 1;
            m_SellProductHistory = m_ProductService.GetSellProductInDay(m_ProductId, null, null);
            CalculateTotalSellAmount();
            BindSellHistoryToGrid();
        }

        private void ChangePageSellHistory()
        {
            BindSellHistoryToGrid();
        }

        private void btnSellFirst_Click(object sender, EventArgs e)
        {
            m_SellPageNumber = 1;
            ChangePageSellHistory();
        }

        private void btnSellPrev_Click(object sender, EventArgs e)
        {
            m_SellPageNumber = m_SellPageNumber > 1 ? m_SellPageNumber - 1 : m_SellPageNumber;
            ChangePageSellHistory();
        }

        private void mnrSellPageIndex_ValueChanged(object sender, EventArgs e)
        {
            if (m_EnabledSellEvent)
            {
                m_SellPageNumber = Convert.ToInt32(mnrSellPageIndex.Value);
                ChangePageSellHistory();
            }
        }

        private void btnSellNext_Click(object sender, EventArgs e)
        {
            m_SellPageNumber = m_SellPageNumber < m_SellMaxPage ? m_SellPageNumber + 1 : m_SellPageNumber;
            ChangePageSellHistory();
        }

        private void btnSellLast_Click(object sender, EventArgs e)
        {
            m_SellPageNumber = m_SellMaxPage;
            ChangePageSellHistory();
        }

        private void SetEnabledForSellPagingNavigator()
        {
            if (m_SellPageNumber == m_SellMaxPage)
            {
                if (m_SellPageNumber == 1)
                {
                    btnSellFirst.Enabled = false;
                    btnSellPrev.Enabled = false;
                    btnSellLast.Enabled = false;
                    btnSellNext.Enabled = false;
                }
                else
                {
                    btnSellFirst.Enabled = true;
                    btnSellPrev.Enabled = true;
                    btnSellLast.Enabled = false;
                    btnSellNext.Enabled = false;
                }
            }
            else if (m_SellPageNumber == 1)
            {
                btnSellFirst.Enabled = false;
                btnSellPrev.Enabled = false;
                btnSellLast.Enabled = true;
                btnSellNext.Enabled = true;
            }
            else
            {
                btnSellFirst.Enabled = true;
                btnSellPrev.Enabled = true;
                btnSellLast.Enabled = true;
                btnSellNext.Enabled = true;
            }
        }
        #endregion Sell Product
    }
}
